//
//  UIScrollView+TGMPullToRefresh.m
//  WengWeng5
//
//  Created by HanFeng on 14-3-17.
//  Copyright (c) 2014年 mafengwo. All rights reserved.
//

#import "UIScrollView+TGMPullToRefresh.h"
#import <objc/runtime.h>

static void *__tgm_pull_to_refresh_delegate__;

@implementation UIScrollView (TGMPullToRefresh)

- (id<TGMPullToRefreshDelegate>)tgm_delegateForPullToRefresh
{
    return objc_getAssociatedObject(self, &__tgm_pull_to_refresh_delegate__);
}

- (void)setTgm_delegateForPullToRefresh:(id<TGMPullToRefreshDelegate>)delegateForPullToRefresh
{
    objc_setAssociatedObject(self, &__tgm_pull_to_refresh_delegate__, delegateForPullToRefresh, OBJC_ASSOCIATION_ASSIGN);
}

- (void)tgm_initializePullToRefreshWithDelegate:(id<TGMPullToRefreshDelegate>)delegateForPullToRefresh
                                       userInfo:(id)userInfo
{
    self.tgm_delegateForPullToRefresh = delegateForPullToRefresh;
    if (delegateForPullToRefresh) {
        [self __TGM_initializeSVPullToRefresh:userInfo];
//        [self __TGM_initializeSVInfiniteScrolling:userInfo];
    }
}

- (void)tgm_clearPullToRefresh
{
    self.tgm_delegateForPullToRefresh = nil;
    [self clearAllPullToRefreshIssues];
    [self clearAllInfiniteScrollingIssues];
}

- (void)tgm_stopPullToRefreshAnimating
{
    [self.pullToRefreshView stopAnimating];
}

- (void)tgm_stopLoadMoreAnimating
{
    [self.infiniteScrollingView stopAnimating];
}

- (TGMBasePullToRefreshView *)tgm_refreshView
{
    return (id)[self.pullToRefreshView customView];
}

- (TGMBaseLoadMoreView *)tgm_loadMoreView
{
    return (id)[self.infiniteScrollingView customView];
}

#pragma mark - Internal Methods

- (void)__TGM_initializeSVPullToRefresh:(id)userInfo
{
    __weak UIScrollView *weakSelf = self;
    
    NSInteger tag = (NSInteger)(__bridge void *)self;
    
    // pull to refresh
    if (self.pullToRefreshView == nil || self.pullToRefreshView.tag != tag) {
        [self addPullToRefreshWithActionHandler:^(BOOL byUser){
            NSLog(@"下拉刷新~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %@", byUser?@"用户":@"程序");
            [weakSelf.tgm_delegateForPullToRefresh tgm_ScrollViewDidPullToRefresh:weakSelf byUser:byUser];
        } expectedContentInsetGetterInNormalState:^UIEdgeInsets{
            if (weakSelf.tgm_delegateForPullToRefresh == nil) {
                return weakSelf.contentInset;
            }
            return [weakSelf.tgm_delegateForPullToRefresh tgm_ScrollViewExpectedContentInset:weakSelf];
        } viewHeight:60 verticalOffset:0];
        
        Class clazz = [self.tgm_delegateForPullToRefresh tgm_classForPullToRefreshView:self];
        NSCAssert([clazz isSubclassOfClass:[TGMBasePullToRefreshView class]], @"要求%@的下拉刷新view为TGMBasePullToRefreshView或其子类", self);
        TGMBasePullToRefreshView *v = [[clazz alloc] initWithFrame:(CGRect){0,0,self.boundsWidth,60}];
        v.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;
        v.userInfo = userInfo;
        [self.pullToRefreshView setCustomView:v];
        
        self.pullToRefreshView.tag = tag;
    }
}

- (void)__TGM_initializeSVInfiniteScrolling:(id)userInfo
{
    __weak UIScrollView *weakSelf = self;
    
    NSInteger tag = (NSInteger)(__bridge void *)self;
    
    // load more
    if (self.infiniteScrollingView == nil | self.infiniteScrollingView.tag != tag) {
        [self addInfiniteScrollingWithActionHandler:^{
            [weakSelf.tgm_delegateForPullToRefresh tgm_ScrollViewDidLoadMore:weakSelf];
        } expectedContentInsetGetter:^UIEdgeInsets{
            if (weakSelf.tgm_delegateForPullToRefresh == nil) {
                return weakSelf.contentInset;
            }
            return [weakSelf.tgm_delegateForPullToRefresh tgm_ScrollViewExpectedContentInset:weakSelf];
        } viewHeight:60 expectedTriggerOffset:30];
        
        Class clazz = [self.tgm_delegateForPullToRefresh tgm_classForLoadMoreView:self];
        NSCAssert([clazz isSubclassOfClass:[TGMBasePullToRefreshView class]], @"要求%@的加载更多view为TGMBaseLoadMoreView或其子类", self);
        TGMBaseLoadMoreView *v = [[clazz alloc] initWithFrame:(CGRect){0,0,self.boundsWidth,40}];
        v.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleBottomMargin;
        v.userInfo = userInfo;
        [self.infiniteScrollingView setCustomView:v];
        
        self.infiniteScrollingView.tag = tag;
    }
    self.showsInfiniteScrolling = NO;
}

@end
